package itbour.onetouchshow.utils.gsonanalysis;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

import itbour.onetouchshow.bean.inching.DocContentV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingDocStringNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTextValueV10Bean;
import itbour.onetouchshow.bean.ppt.InchingDoStringPPTV10Bean;
import itbour.onetouchshow.constants.platform.DataPlatform;

/**
 * Created by zjl_d on 2017/11/22.
 */

public class DocStringObjToJsonCommonRules implements JsonSerializer<DocContentV10Bean> {
    @Override
    public JsonElement serialize(DocContentV10Bean obj, Type typeOfSrc, JsonSerializationContext context) {
        /**
         * 1.去除本地字段pageIndexAndObjIdList(不在转换时加上这个属性 就是去掉了)
         * 2.判断是否是ppt(已判断)
         * 3.将root转为docString(按照opType判断添加自定义解析规则)
         */
        JsonObject json = new JsonObject();
        json.addProperty("docId", obj.getDocId());
        json.addProperty("tmplId", obj.getTmplId());

        int opType = obj.getOpType();
        json.addProperty("opType", opType);
        if (opType == DataPlatform.ORTYPE_PPT) {
            InchingDoStringPPTV10Bean pptCoreData = obj.getPptCoreData();
        } else {

            InchingDocStringNoPPTV10Bean noPptCoreData = obj.getNoPptCoreData();

            String coreData = new GsonBuilder()
                    .registerTypeAdapter(InchingCoreRootNoPPTV10Bean.class, new CoreObjToJsonNoPPTRules())
                    .registerTypeAdapter(NoPPTRootTextValueV10Bean.class, new RootTextValueV10RemoveToJsonRules())
                    .create().toJson(noPptCoreData);

            json.addProperty("docString", coreData);
        }

        return json;
    }
}
